package cn.com.dcsgo.controller;

import cn.com.dcsgo.common.Result;
import cn.com.dcsgo.domain.dto.SysDictDataSaveDTO;
import cn.com.dcsgo.domain.dto.SysDictDataUpdateDTO;
import cn.com.dcsgo.domain.po.SysDictData;
import cn.com.dcsgo.permission.MyPermission;
import cn.com.dcsgo.service.SysDictDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * SysDictDataController
 *
 * @author Dcsgo
 * @since 24/5/2025 上午 10:28
 */
@Slf4j
@Tag(name = "字典数据")
@RestController
@RequestMapping("sysDictData")
@RequiredArgsConstructor
public class SysDictDataController extends BaseController {
    private final SysDictDataService sysDictDataService;

    @MyPermission({"system:dictData:add"})
    @Operation(summary = "添加字典数据")
    @PostMapping("save")
    public Result<Serializable> save(@Validated @RequestBody SysDictDataSaveDTO saveDTO) {
        int row = sysDictDataService.save(saveDTO);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:dictData:update"})
    @Operation(summary = "修改字典数据")
    @PutMapping("update")
    public Result<Serializable> update(@Validated @RequestBody SysDictDataUpdateDTO updateDTO) {
        int row = sysDictDataService.update(updateDTO);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:dictData:query"})
    @Operation(summary = "获取字典数据", description = "根据字典数据编号获取字典数据")
    @GetMapping("get/{dictCode}")
    public Result<SysDictData> get(@PathVariable Long dictCode) {
        return new Result<>(sysDictDataService.get(dictCode));
    }

    @MyPermission({"system:dictData:query"})
    @Operation(summary = "获取字典数据(字典类型)", description = "根据字典类型获取字典数据")
    @GetMapping("getDictDataByDictType/{dictType}")
    public Result<List<SysDictData>> get(@PathVariable String dictType) {
        return new Result<>(sysDictDataService.get(dictType));
    }

    @MyPermission({"system:dictData:remove"})
    @Operation(summary = "删除字典数据", description = "根据字典数据编号删除字典数据")
    @DeleteMapping("delete/{dictCode}")
    public Result<Serializable> remove(@PathVariable Long dictCode) {
        int row = sysDictDataService.remove(dictCode);
        return chooseReturn(row > 0);
    }
}
